﻿#include "dwgcircleitem.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QtMath>
#include <QGraphicsSceneMouseEvent>

DwgCircleItem::DwgCircleItem() {}

DwgCircleItem::DwgCircleItem(QPointF pt1, QPointF pt2)
{
    mCenPt=pt1;
    mRad = sqrt(pow(pt2.x()-pt1.x(),2)+pow(pt2.y()-pt1.y(),2));
}

QRectF DwgCircleItem::boundingRect() const
{
    return QRectF(mCenPt.x()-mRad,mCenPt.y()-mRad,mRad*2,mRad*2);//.adjusted(-5,-5,5,5);
}

void DwgCircleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(option->state & QStyle::State_Selected)
    {
        DrawSnapRect(painter,mCenPt);
        QPointF pt1,pt2,pt3,pt4;
        pt1 = mCenPt + QPointF(mRad,0);
        pt2 = mCenPt + QPointF(0,mRad);
        pt3 = mCenPt + QPointF(-mRad,0);
        pt4 = mCenPt + QPointF(0,-mRad);
        DrawSnapRect(painter,pt1);
        DrawSnapRect(painter,pt2);
        DrawSnapRect(painter,pt3);
        DrawSnapRect(painter,pt4);
    }
    QPen pen(mColor, mLineWidth, mPenStyle);
    painter->setPen(pen);
    painter->drawEllipse(boundingRect());
}

void DwgCircleItem::DrawSnapRect(QPainter *painter, QPointF cpt)
{
    painter->save();
    QBrush brush(Qt::blue);
    painter->setBrush(brush);
    painter->drawRect(cpt.x()-5,cpt.y()-5,10,10);
    painter->restore();
}

int DwgCircleItem::Snaped(QPointF spt)
{
    int n = 0;

    qreal dist;
    dist = sqrt(pow((mCenPt.x()-spt.x()),2)+pow((mCenPt.y()-spt.y()),2));
    if(dist < 8)
        n = 1;   // 捕捉到圆心

    QPointF pt1,pt2,pt3,pt4;    // 定义4个象限点，用来判断是否捕捉到
    pt1 = mCenPt + QPointF(mRad,0);
    pt2 = mCenPt + QPointF(0,mRad);
    pt3 = mCenPt + QPointF(-mRad,0);
    pt4 = mCenPt + QPointF(0,-mRad);

    dist = sqrt(pow((pt1.x()-spt.x()),2)+pow((pt1.y()-spt.y()),2));
    if(dist < 8)
        n = 2;   // 捕捉到圆的第1象限点
    dist = sqrt(pow((pt2.x()-spt.x()),2)+pow((pt2.y()-spt.y()),2));
    if(dist < 8)
        n = 3;   // 捕捉到圆的第2象限点
    dist = sqrt(pow((pt3.x()-spt.x()),2)+pow((pt3.y()-spt.y()),2));
    if(dist < 8)
        n = 4;   // 捕捉到圆的第3象限点
    dist = sqrt(pow((pt4.x()-spt.x()),2)+pow((pt4.y()-spt.y()),2));
    if(dist < 8)
        n = 5;   // 捕捉到圆的第4象限点
    return n;
}

int snapPtn = 0;
void DwgCircleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{

    if(event->buttons() & Qt::LeftButton)
    {
        snapPtn = Snaped(event->scenePos());
    }

    QGraphicsItem::mousePressEvent(event);
}

void DwgCircleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{

    QPointF curPt = event->scenePos();
    switch(snapPtn)
    {
    case 1:
        mCenPt = curPt;
        break;
    case 2:
    case 3:
    case 4:
    case 5:
        mRad = sqrt(pow(mCenPt.x()-curPt.x(),2)+pow(mCenPt.y()-curPt.y(),2));
        break;
    default:
        break;
    }

    this->update();
}

QPainterPath DwgCircleItem::shape() const
{
    QPainterPath path;
    path.addEllipse(boundingRect());
    path.addEllipse(boundingRect().adjusted(10,10,-10,-10));
    path.addEllipse(QRectF(mCenPt.x()-5,mCenPt.y()-5,10,10));
    return path;
}
